package com.jiot.controller.user;

import com.zg.cloud.BaseResponse;
import com.zg.cloud.CommonEnum.Role;
import com.zg.cloud.annotation.RoleCheck;
import com.zg.cloud.annotation.TokenCheck;
import com.zg.cloud.service.UserAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import req.admin.user.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author liling
 * @date 2020/11/16 14:20
 * @description
 **/
@RestController
@RequestMapping("/user/admin")
@Api(value = "管理端用户模块")
public class UserAdminController {

    @Resource
    private UserAdminService userAdminService;

    @PostMapping("/login")
    @ApiOperation(value = "管理员用户登录", notes = "user")
    public BaseResponse login(@RequestBody @Valid AdminUserLoginReq adminUserLoginReq) {
        return new BaseResponse(userAdminService.login(adminUserLoginReq));
    }

    @RoleCheck({Role.SYS_ADMIN, Role.ADMIN, Role.PROVINCE_ADMIN, Role.CITY_ADMIN, Role.DISTRICT_ADMIN})
    @TokenCheck
    @PostMapping("/user/list")
    @ApiOperation(value = "用户列表", notes = "user")
    public BaseResponse userList(@RequestBody @Valid GetUserListReq getUserListReq) {
        return new BaseResponse(userAdminService.userList(getUserListReq));
    }

    @RoleCheck({Role.SYS_ADMIN, Role.ADMIN, Role.PROVINCE_ADMIN, Role.CITY_ADMIN, Role.DISTRICT_ADMIN})
    @TokenCheck
    @PostMapping("/user/add")
    @ApiOperation(value = "新增用户", notes = "user")
    public BaseResponse addUser(@RequestBody @Valid AddUserReq addUserReq) {
        userAdminService.addUser(addUserReq);
        return new BaseResponse();
    }

    @RoleCheck({Role.SYS_ADMIN, Role.ADMIN, Role.PROVINCE_ADMIN, Role.CITY_ADMIN, Role.DISTRICT_ADMIN})
    @TokenCheck
    @PostMapping("/user/delete")
    @ApiOperation(value = "删除用户", notes = "user")
    public BaseResponse userDelete(@RequestBody @Valid DeleteUserReq deleteUserReq) {
        userAdminService.delete(deleteUserReq);
        return new BaseResponse();
    }

    @RoleCheck({Role.SYS_ADMIN, Role.ADMIN, Role.PROVINCE_ADMIN, Role.CITY_ADMIN, Role.DISTRICT_ADMIN})
    @TokenCheck
    @PostMapping("/user/update/info")
    @ApiOperation(value = "修改用户获取详情", notes = "user")
    public BaseResponse userUpdateInfo(@RequestBody @Valid GetUserInfoReq getUserInfoReq) {
        return new BaseResponse(userAdminService.updateInfo(getUserInfoReq));
    }

    @RoleCheck({Role.SYS_ADMIN, Role.ADMIN, Role.PROVINCE_ADMIN, Role.CITY_ADMIN, Role.DISTRICT_ADMIN})
    @TokenCheck
    @PostMapping("/user/update")
    @ApiOperation(value = "修改用户信息", notes = "user")
    public BaseResponse userUpdate(@RequestBody @Valid UpdateUserReq updateUserReq) {
        userAdminService.update(updateUserReq);
        return new BaseResponse();
    }

    @RoleCheck({Role.SYS_ADMIN, Role.ADMIN, Role.PROVINCE_ADMIN, Role.CITY_ADMIN, Role.DISTRICT_ADMIN})
    @TokenCheck
    @PostMapping("/user/count")
    @ApiOperation(value = "用户数量", notes = "user")
    public BaseResponse userCount() {
        return new BaseResponse(userAdminService.count());
    }
}
